package com.pearadmin.system.timedTask;

import com.alibaba.fastjson.JSON;
import com.pearadmin.common.tools.datetime.DateUtil;
import com.pearadmin.system.domain.SysAxis;
import com.pearadmin.system.domain.SysAxisShake;
import com.pearadmin.system.service.ISysAxisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.List;

/**
 * 定时获得主轴震动值，并发送到前端
 */
@Slf4j
@Component //把此类托管给Spring
public class ShakeTimedTask {

    @Resource
    private ISysAxisService sysAxisService;

    @Scheduled(cron = "0/1 * * * * ? ")
    public void saveShakeRedis(){
        System.out.println("连接Jedis");
        Jedis jedis=new Jedis();
        jedis.auth("root");
        List<String> axisId=sysAxisService.selectAllAxisCount();
        SysAxisShake sysAxisShake=new SysAxisShake();
        // 2.增强的for循环
        //test
        for (String str : axisId) {
            SysAxis sysAxis= sysAxisService.selectSysAxisById(Long.parseLong(str));
            double num=Math.random()*(10-1)+1;
            double num2=Math.random()*(10-1)+1;
            sysAxisShake.setShakeA(num);
            sysAxisShake.setShakeB(num2);
            sysAxisShake.setAxisType(sysAxis.getAxisType());
            String dateTime=JSON.toJSONString(DateUtil.getNowDate());
//            System.out.println(dateTime);
            jedis.zadd(str,Double.parseDouble(dateTime), JSON.toJSONString(sysAxisShake));
            jedis.close();
        }
    }
}
